local base = piece "base"
local jammersturret = piece "jammersturret"
local deploy = piece "deploy"
local pump1 = piece "pump1"
local pump2 = piece "pump2"
local jam1 = piece "jam1"
local jam2 = piece "jam2"
local jam3 = piece "jam3"
local bigjam1 = piece "bigjam1"
local bigjam2 = piece "bigjam2"
local bigjam3 = piece "bigjam3"
local cylinder = piece "cylinder"
local turret = piece "turret"

local function pumpone()
        while( true ) do
        Turn(pump1, z_axis, 0.05, 0.1) 
        WaitForTurn(pump1, z_axis)
        Turn(pump1, z_axis, -0.05, 0.1)
	WaitForTurn(pump1, z_axis)
        end
end

local function pumptwo()  
        while( true ) do
        Turn(pump2, x_axis, 0.05, 0.1)
        WaitForTurn(pump2, x_axis)   
        Turn(pump2, x_axis, -0.05, 0.1)
        WaitForTurn(pump2, x_axis)
        end
end


local function Open()
        Signal(2)
        SetSignalMask(1)

	Move(deploy, y_axis, 30, 10)

	Turn(jam1, z_axis, -0.8, 0.5)
	Turn(jam2, z_axis, -0.8, 0.5)
	Turn(jam3, z_axis, -0.8, 0.5)

	Turn(bigjam1, x_axis, -0.2, 0.1)
	Turn(bigjam2, x_axis, -0.2, 0.1)
	Turn(bigjam3, x_axis, -0.2, 0.1)

        Spin(jammersturret, y_axis, 0.3, 0.01)
        Spin(turret, y_axis, -0.5, 0.01)
        Spin(cylinder, y_axis, 1.2, 0.05)

        StartThread(pumpone)
        StartThread(pumptwo)
	
        return true
end

local function Close()
        Signal(1)
        SetSignalMask(2)

	Move(deploy, y_axis, 0, 10)

        Turn(jam1, z_axis, 0, 0.5)
        Turn(jam2, z_axis, 0, 0.5)
        Turn(jam3, z_axis, 0, 0.5)

	Turn(bigjam1, x_axis, 0, 0.1)
	Turn(bigjam2, x_axis, 0, 0.1)
	Turn(bigjam3, x_axis, 0, 0.1)

        StopSpin(jammersturret, y_axis, 0.01)
        StopSpin(turret, y_axis, 0.01)
        StopSpin(cylinder, y_axis, 0.01)
        return true
end

function script.Create ( )
	Turn(jam2, y_axis, 2.0943951)
	Turn(jam3, y_axis, -2.0943951)
	
	Turn(bigjam2, y_axis, 2.0943951)
	Turn(bigjam3, y_axis, -2.0943951)
end

function script.Activate ( )
        StartThread( Open )

end

function script.Deactivate ( )
        StartThread( Close )
end
